import collections
import os
import pandas
import logging

from absl import app

from coin.exchange.base.strategy_logger import parse_file_name

ApplicationHistory = collections.namedtuple(
    'ApplicationHistory',
    ['application', 'account', 'exchange', 'market_type', 'datestr', 'timestr', 'machine'])


# hack
def get_application_history_from_files(files, use_custom_parse=True):
  records = []
  for file in files:
    print(file)
    if file.find("201909") < 0:
      continue
    # parse_file_name won't work well. why?
    if use_custom_parse:
      file_iter = file.replace(".gz", "")
      # very hack. do not try this at home.
      #'pass_unhedge_2.py.Bitmex.Futures.20180531-105036'

      datetimestr = file_iter[file_iter.rfind("."):][1:]
      if datetimestr.find(
          "-"
      ) < 0:  # then timestamp part. pass_unhedge_2.py.xunke07.Futures.Bitmex.20180613-140834.1528898914455790000
        file_iter = file_iter[:file_iter.rfind(".")]
        datetimestr = file_iter[file_iter.rfind("."):][1:]

      if datetimestr.find("-") < 0:
        # parse error. but usually look like this:
        # unhedged_mm.py.xunke07.Spot.Okex.20180913-233344.1536881624213903000.e8dFF28a
        continue

      datestr, timestr = datetimestr.split("-")

      file_iter = file_iter[:file_iter.rfind(".")]

      # lm_agg.py.prestoinvt09.Futures.Huobi.20190804-075415.1564905255065406000.gz
      exchange = file_iter[file_iter.rfind("."):][1:]
      file_iter = file_iter[:file_iter.rfind(".")]

      market_type = file_iter[file_iter.rfind("."):][1:]
      file_iter = file_iter[:file_iter.rfind(".")]

      account = None
      # shit the hack.. my eyes..
      if not file_iter.endswith(".py"):
        account = file_iter[file_iter.rfind("."):][1:]
        file_iter = file_iter[:file_iter.rfind(".")]

      application = file_iter
      # intentionally omit timestr.

      # shit.....!?
      if exchange in ['Futures', 'Spot']:
        records.append(
            ApplicationHistory(application, account, market_type, exchange, datestr, None, None))
      else:
        records.append(
            ApplicationHistory(application, account, exchange, market_type, datestr, None, None))
    else:
      try:
        parsed = parse_file_name(file)
        import pdb
        pdb.set_trace()
      except Exception:
        pass

  return list(set(records))


def generate_run_history_hack():
  # hack2
  allrecords = []
  for i, (fullpath, dirname, files) in enumerate(os.walk("/remote/iosg/strat-1/buckets/log.raw.coin/live/og_proto_log")):
    if len(files) > 0:
      records = get_application_history_from_files(files)
      # /remote/iosg/strat-1/buckets/log.raw.coin/live/og_proto_log/strategy-11.eu-west-1/20180531
      # above is fullpath. do not assume this at home.
      machine = fullpath.split("/")[-2]
      records = [record._replace(machine=machine) for record in records]
      allrecords.extend(records)
  rhdf = pandas.DataFrame(allrecords)
  export_csv = "/remote/iosg/data/etc/shared/jhkim/strat_record.csv"
  rhdf.to_csv(export_csv)


def main(_):
  generate_run_history_hack()


if __name__ == '__main__':
  logging.basicConfig(level='DEBUG', format='%(levelname)8s %(asctime)s %(name)s] %(message)s')
  app.run(main)
